package org.lgiavedoni.android.franq.sync;

import org.lgiavedoni.android.franq.core.calendar.Calendar;
import org.lgiavedoni.android.franq.core.calendar.CalendarEvent;
import org.lgiavedoni.android.franq.core.calendar.RegisterCalendarEvent;
import org.lgiavedoni.android.franq.core.prediction.Prediction;
import org.lgiavedoni.android.franq.core.sync.AbstractSyncProcess;
import org.lgiavedoni.android.franq.core.sync.Synchronizable;
import org.lgiavedoni.android.franq.service.config.ConfigService;
import org.lgiavedoni.android.franq.service.config.ConfigService.ConfigKey;
import org.lgiavedoni.android.franq.service.phone.calendar.CalendarService;

public class PredictionsSyncProcess extends AbstractSyncProcess {

	private Calendar predictionsCalendar;

	private Calendar getPredictionsCalendar() {
		if (predictionsCalendar == null)
			predictionsCalendar = CalendarService.inst.getOrCreateCalendar(ConfigService.inst.getValue(ConfigKey.PREDICTION_CALENDAR_NAME));
		return predictionsCalendar;
	}

	@Override
	public boolean init() {

		for (CalendarEvent ce : CalendarService.inst.getFutureCalendarEvents(getPredictionsCalendar())) {
			CalendarService.inst.removeCalendarEvent(ce);
		}

		return true;
	}

	@Override
	public boolean syncronizeObj(Synchronizable obj) {
		if (obj instanceof RegisterCalendarEvent) {
			CalendarService.inst.addCalendarEvent(getPredictionsCalendar(), ((RegisterCalendarEvent) obj).toCalendarEvent());
			return true;
		}
		return false;
	}

	@Override
	protected void resetInternal() {
		CalendarService.inst.removeAllCalendarEvents(getPredictionsCalendar());
	}

	@Override
	public Class getObjectClass() {
		return Prediction.class;
	}

}
